package com.monkeygroup.mygoplus.ecchat.common.utils;

import android.content.Context;
import android.widget.ImageView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.ecchat.common.CCPAppManager;
import com.monkeygroup.mygoplus.ecchat.storage.ContactSqlManager;
import com.monkeygroup.mygoplus.ecchat.ui.contact.ECContacts;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.yuntongxun.ecsdk.ECVoIPCallManager;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/11.
 */
public class FunUtil {

    /**
     * 發送添加好友請求
     **/
    public static void addFriend(final String friendId) {
        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestAddFriend;

        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);
        params.put("friendMobile", friendId);
        HttpModel httpModel = new HttpModel(requestUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, String> map = (Map) data;
                    ToastUtil.showMessage(map.get("message"));
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    /**
     * 取消關注
     **/
    public static void doCancelConcern(final String userid, final ImageView imageView) {
        if (MyGoApplication.myECUserid.equals(""))
            return;

        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);
        params.put("concernMobile", userid);

        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestCancelConcern;
        HttpModel httpModel = new HttpModel(requestUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, String> map = (Map) data;
                    ToastUtil.showMessage(map.get("message"));
                    imageView.setImageResource(R.drawable.bt_chat_favorite_invalid);
                    MyGoApplication.getConcernList().remove(userid);
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    /**
     * 關注
     **/
    public static void doConcern(final String userid, final ImageView imageView) {
        if (MyGoApplication.myECUserid.equals(""))
            return;

        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);
        params.put("concernMobile", userid);

        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestConcernUrl;
        HttpModel httpModel = new HttpModel(requestUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, String> map = (Map) data;
                    ToastUtil.showMessage(map.get("message"));
                    imageView.setImageResource(R.drawable.bt_chat_favorite_ok);
                    MyGoApplication.getConcernList().put(userid, MyGoApplication.getContacts().get(userid));
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    public static void handleVoiceCall(Context mContext, String userid) {
        ECContacts contact = ContactSqlManager.getContact(userid);
        if (contact == null) {
            return;
        }
        CCPAppManager.callVoIPAction(mContext, ECVoIPCallManager.CallType.VOICE,
                contact.getNickname(), contact.getContactid(), false);

    }

    public static void handleVideoCall(Context mContext, String userid) {
        ECContacts contact = ContactSqlManager.getContact(userid);

        if (contact == null) {
            return;
        }
        CCPAppManager.callVoIPAction(mContext, ECVoIPCallManager.CallType.VIDEO,
                contact.getNickname(), contact.getContactid(), false);

    }

}
